package com.utils;

import java.util.Random;

public class CreatePasswd {

    public static String getRandomPassword(int len) {
        String result = null;
        while (len >= 6) {
            result = makeRandomPassword(len);
            if (result.matches(".*[a-z]{1,}.*") && result.matches(".*[A-Z]{1,}.*") && result.matches(".*\\d{1,}.*") && result.matches(".*[~!@#$%^&*\\.?]{1,}.*")) {
                return result;
            }
        }
        return "长度不得少于6位!";
    }

    public static String makeRandomPassword(int len) {
        char charr[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123567890~!@#$%^&*.?".toCharArray();
        StringBuilder sb = new StringBuilder();
        Random r = new Random();
        for (int x = 0; x < len; ++x) {
            sb.append(charr[r.nextInt(charr.length)]);
        }
        return sb.toString();
    }

    /**
     * 　　	 * 输出区间[a,b]的随机整数
     * 　　	 *
     * 　　	 * @param a,b
     * 　　	 *           随机数区间
     * 　　	 * @return  a到b范围内的随机数
     * 　　	 * @throws JSONException
     *
     */
    public static int generateRandamNum(int a, int b) {
        Random random = new Random();
        int num = a + random.nextInt(b - a + 1);
        return num;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            System.out.println(getRandomPassword(16));
        }

        for (int i = 0; i < 3; i++) {
            System.out.println(getRandomPassword(8));
        }


//        System.out.println(getRandomPassword(generateRandamNum(8,16)));
//        System.out.println((int) ((Math.random() * 9 + 1) * 10000));
    }

}
